package com.zhdl.modules.prefiresys.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.zhdl.common.ytools.DateTimeHelper;
import com.zhdl.common.ytools.HttpClientUtil;
import com.zhdl.modules.prefiresys.entity.DefAreaAlarmRec;
import com.zhdl.modules.prefiresys.service.DefAreaAlarmRecService;
import com.zhdl.modules.prefiresys.service.dto.AlarmRecCriteria;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时上报探测器报警数据
 */
@Slf4j
@Component
public class ReportAlarmRec {

    final DefAreaAlarmRecService alarmRecService;

    @Autowired
    public ReportAlarmRec(DefAreaAlarmRecService alarmRecService) {
        this.alarmRecService = alarmRecService;
    }

    @Async
    public void scheduledTask() {
        log.debug("定时任务 上报发送探测器报警记录数据");
        //testReportAlarmRec();
        reportAlarmRec();
    }

    private void reportAlarmRec() {

        //1.获取待上报数据
        AlarmRecCriteria alarmRecCriteria = new AlarmRecCriteria();
        alarmRecCriteria.setReportStatus(0);
        List<DefAreaAlarmRec> alarmRecList = alarmRecService.findList(alarmRecCriteria);
        if (alarmRecList == null || alarmRecList.size() < 1) return;
        System.out.println("待上报数据数量:" + alarmRecList.size());
        //2.上报数据预处理

        //3.调用上报接口上报数据
        //for(int i=0;i<10;i++)
        for (int i = 0; i < alarmRecList.size(); i++) {
            DefAreaAlarmRec alarmDto = alarmRecList.get(i);
            String jsonString = JSON.toJSONString(alarmDto);
            String urlPath = new String("/api/alarmRec/addAlarmRec");
            //String urlPath=new String("/api/alarmRecSend/sendAlarmRec");
            String returnResult = HttpClientUtil.reportDataByHttp(jsonString, urlPath);
            //4.如果上报成功,更改上报标志
            if (Integer.valueOf(returnResult) == HttpStatus.CREATED.value()) {
                alarmDto.setReportStatus(1);
                alarmRecService.updateIgnoreNull(alarmDto, alarmDto.getId());
            }
        }
    }

    private void testReportAlarmRec() {
        DefAreaAlarmRec alarmDto = new DefAreaAlarmRec();
        alarmDto.setId(IdUtil.fastSimpleUUID());
        alarmDto.setDetectorId(new String("a5a8c81341a54840a9a7ca1f08c4fd18"));
        alarmDto.setAreaId(new String("c9a99e07882a4aeb98456ae0e7558cc6"));
        alarmDto.setAlarmStatus(Integer.valueOf(0).byteValue());
        alarmDto.setNextAlarmStatus(Integer.valueOf(1).byteValue());
        alarmDto.setStartTime(DateUtil.now());
        alarmDto.setEndTime(DateUtil.now());
        alarmDto.setDurationSec(DateTimeHelper.calcDurationSec(alarmDto.getStartTime(), alarmDto.getEndTime()));
        String jsonString = JSON.toJSONString(alarmDto);
        //String urlPath=new String("/api/alarmRec/addAlarmRec");
        String urlPath = new String("/api/alarmRecSend/sendAlarmRec");
        String returnResult = HttpClientUtil.reportDataByHttp(jsonString, urlPath);
    }

}
